<?php  
	class Trend
	{
		private $orders;
		private $count;
		
		//These two variables belong together like a matrix.
		private $daysInMonth;//not sure what i had this for.
		private $dayTotals;//total for each day
		private $trendMonth;//keep month with object so amount of days is known.
		
		public function __construct()
		{
			$orders[0] = 0;	
			$count = 0;
			$dayTotals = 0;
			$month = "";
		}
			
		public function getOrdersForMonth($month)
		{
			$DTB = dbHandler::getInstance();
			$query = "SELECT * FROM orders";
			$result = $DTB->query($query);
			$this->count = 0;
			while($row = mysql_fetch_array($result))
			{
				$itemMonth = $row['order_date'];
				$isCorrectMonth = strncasecmp($month, $itemMonth, 3);
				if($isCorrectMonth == 0)//if month is $month then add it to list
				{
					$this->orders[$this->count] = $row;
					$this->count= $this->count + 1;
				}
			}
			$this->calculateMonthDailyTotals($month);
		}
		
		public function calculateMonthDailyTotals($month)
		{
			$numDaysInMonth = 0;
			$this->trendMonth = $month;
			if($month == "January" || $month == "March" || $month == "May" || $month == "July" || $month == "August" || $month == "October" || $month == "December")
			{
				$numDaysInMonth = 31;
			}
			else if($month == "February")
			{
				$numDaysInMonth = 28;
			}
			else
			{
				$numDaysInMonth = 30;
			}
			$this->daysInMonth = $numDaysInMonth;
				$DTB = dbHandler::getInstance();
				for($i = 1; $i <= $numDaysInMonth; $i++)//loop through days of month
				{
					$itemPrices = 0;
					for($j = 0; $j < $this->count; $j++)//loop through all orders for month
					{
						$date = $this->orders[$j]['order_date'];//get the date
						$date = substr($date, strlen($month)+1, 2);
						//check to ensure that day matches $i
						if($i > 9)
						{
							$isCorrectDay = strncasecmp($date, $i, 2);
						}
						else 
							$isCorrectDay = strncasecmp($date, $i, 1);
							
							
						if($date == $i)
						{
							//Then query orderItem to get all items for that order.
							$orderNumber = $this->orders[$j]['order_no'];
							$query = "SELECT * FROM orderitem WHERE order_no = '$orderNumber'";
							$result = $DTB->query($query);
							//add up total for order.
							while($row = mysql_fetch_array($result))
							{
								$itemQuantity = $row['quantity'];
								$itemPrices = $itemPrices + ($row['price'] * $itemQuantity);
							}
							
						}
						
					}
					//put total into day total.
					$this->dayTotals[$i] = $itemPrices;
				}
			
		}
		
		public function getDayTotals()
		{
			return $this->dayTotals;
		}
		
		public function getMonth()
		{
			return $this->trendMonth;
		}
		public function getDays()
		{
			return $this->daysInMonth;
		}
	}
?>